package org.bonorsoft.modules.statistical.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.bonorsoft.common.util.oConvertUtils;
import org.bonorsoft.modules.education.entity.EduInfor;
import org.bonorsoft.modules.education.service.IEduInforService;
import org.bonorsoft.modules.statistical.entity.AllSchoolVo;
import org.bonorsoft.modules.statistical.entity.FiveSchoolVo;
import org.bonorsoft.modules.statistical.entity.TotalInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/** 功能描述:大屏数据统计
 *
 * @vison: 1.0.0
 * @Author: WilliamYe
 * @Date: 2020/11/25 15:56
 */
@RestController
@RequestMapping("/education/static")
//@Api(tags="大屏数据")
@Slf4j
@CrossOrigin
public class EduStaticController {

    @Autowired
    private IEduInforService eduInforService;

    /** 功能描述:根据年份和学校类型统计数据
     *
     * @vison: 1.0.0
     * @Author: WilliamYe
     * @Date: 2020/11/25 15:56
     */
    //@ApiOperation("根据年份和学校类型统计数据")
    @GetMapping(value = "dongyingEducation")
    public TotalInfo getIndex(@ApiParam(name = "type", value = "学校类型(0-幼儿园；1-普通小学；2-普通初中；3-普通高中；4-特殊教育)") @RequestParam(name="type",required=false) String type,
                              @ApiParam(name = "year", value = "年份", defaultValue = "2019") @RequestParam(name="year",defaultValue="2019") Integer year){
        return eduInforService.getStaticInfo(year, type);
    }

    /**
     * 学校信息
     * @param type
     * @param year
     * @return
     */
    @GetMapping(value = "schoolInformation")
    public AllSchoolVo getAllInformation(@RequestParam(name="type",required=false) String type,
                                         @RequestParam(name="year",defaultValue="2019") Integer year){
        return eduInforService.getAllInformation(year, type);
    }

    /**
     * 全市各学科分布
     * @param type
     * @param year
     * @return
     */
    @GetMapping(value = "subjectTeachers")
    public Map<String,Object> getSubjectTeachers(@RequestParam(name="type",required=false) String type,
                                                      @RequestParam(name="year",defaultValue="2019") Integer year){
        Map<String,Object> map = new HashMap<>();
        List<Map<String, Object>> subjectTeachers = eduInforService.getSubjectTeachers(year, type);
        map.put("全市各学科分布",subjectTeachers);
        return map;
    }

    /**
     * 近5年各县区学生数量变化
     * @param type
     * @return
     */
    @GetMapping(value = "five")
    public FiveSchoolVo getFiveStudent(@RequestParam(name="type",required=false) String type){
        return eduInforService.getFiveStudent(type);
    }


    /**
     * 各县区师生比
     * @param year
     * @return
     */
    @GetMapping(value = "ratio")
    public Map<String, Object> getTeacherStudentRatio(@RequestParam(name="year",defaultValue="2019") Integer year){
        return eduInforService.getTeacherStudentRatio(year);
    }

    /**
     * 测试多线程
     */
    @GetMapping("/async")
    public void async() throws InterruptedException, ExecutionException {
        // Start the clock
        long start = System.currentTimeMillis();

        // Kick of multiple, asynchronous lookups
        CompletableFuture<String> page1 = eduInforService.findUser("PivotalSoftware");
        CompletableFuture<String> page2 = eduInforService.findUser("CloudFoundry");
        CompletableFuture<String> page3 = eduInforService.findUser("Spring-Projects");

        // Wait until they are all done
        //join() 的作用：让“主线程”等待“子线程”结束之后才能继续运行
        CompletableFuture.allOf(page1,page2,page3).join();

        // Print results, including elapsed time
        float exc = (float)(System.currentTimeMillis() - start)/1000;
        log.info("Elapsed time: " + exc + " seconds");
        log.info("--> " + page1.get());
        log.info("--> " + page2.get());
        log.info("--> " + page3.get());
    }
}
